﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pomodoro.Core.Model.Domain;

namespace Pomodoro.Core.Model
{
    public class TaskModel
    {

        private static List<Task> DB = new List<Task>();

        public void SaveNewTask(string description) {
            int newID = 0;
            if (DB.Count > 0)
                newID = (from t in DB select t.ID).Max() + 1;
            else
                newID = 1;

            Task newTask = new Task(newID, description);
            DB.Add(newTask);
        }

        public ICollection<Task> AllTasks()
        {
            return DB;
        }

        public Task FindTaskByDescription(string description)
        {
            return (from t in DB 
                    where t.Description.Equals(description) 
                    select t).FirstOrDefault<Task>();
        }

        public bool ExistsTask(int ID)
        {
            return DB.Exists(t => t.ID == ID);
        }

        public int DeleteTask(string canceledTask)
        {
            return DB.RemoveAll(t=> t.Description.Equals(canceledTask));
        }

        public void StartTask(int taskID)
        {
            if (this.ExistsTask(taskID))
                DB.First(t => t.ID == taskID).Start();
        }

        public void EndTask(int taskID)
        {
            if (this.ExistsTask(taskID))
                DB.First(t => t.ID == taskID).End();
        }

        public bool ExistsStartedTask(int taskID)
        {
            return DB.Exists(t => t.Started == true);
        }

        public void UpdateTask(Task taskStated)
        {
            if (ExistsTask(taskStated.ID)){
                DB.Remove(taskStated);
                DB.Add(taskStated);
            }
        }

        public void Clear()
        {
            DB.Clear();
        }
    }
}
